home *** CD-ROM | disk | FTP | other *** search
/ InterCD 2000 September / september_2000.iso / intercd / root / ^Linux / cdrtools-1.10 / lib / stdio / fgetline.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-05-09  |  1.6 KB  |  71 lines

  1. /* @(#)fgetline.c    1.3 96/05/09 Copyright 1986 J. Schilling */
  2. /*
  3.  *    Copyright (c) 1986 J. Schilling
  4.  */
  5. /* This program is free software; you can redistribute it and/or modify
  6.  * it under the terms of the GNU General Public License as published by
  7.  * the Free Software Foundation; either version 2, or (at your option)
  8.  * any later version.
  9.  *
  10.  * This program is distributed in the hope that it will be useful,
  11.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.  * GNU General Public License for more details.
  14.  * 
  15.  * You should have received a copy of the GNU General Public License
  16.  * along with this program; see the file COPYING.  If not, write to
  17.  * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. 
  18.  */
  19.  
  20. #include <stdio.h>
  21. #include "io.h"
  22.  
  23. /*
  24.  * XXX should we check if HAVE_USG_STDIO is defined and
  25.  * XXX use something line memccpy to speed things up ???
  26.  */
  27.  
  28. int fgetline(f, buf, len)
  29.     register    FILE    *f;
  30.             char    *buf;
  31.     register    int    len;
  32. {
  33.     register int    c    = '\0';
  34.     register char    *bp    = buf;
  35.     register int    nl    = '\n';
  36.  
  37.     down2(f, _IOREAD, _IORW);
  38.  
  39.     for(;;) {
  40.         if((c = getc(f)) < 0)
  41.             break;
  42.         if(c == nl)
  43.             break;
  44.         if (--len > 0) {
  45.             *bp++ = c;
  46.         } else {
  47.             /*
  48.              * Read up to end of line
  49.              */
  50.             while ((c = getc(f)) >= 0 && c != nl)
  51.                 ;
  52.             break;
  53.         }
  54.     }
  55.     *bp = '\0';
  56.     /*
  57.      * If buffer is empty and we hit EOF, return EOF
  58.      */
  59.     if(c < 0 && bp == buf)
  60.         return (c);
  61.  
  62.     return (bp - buf);
  63. }
  64.  
  65. int getline(buf, len)
  66.     char    *buf;
  67.     int    len;
  68. {
  69.     return (fgetline(stdin, buf, len));
  70. }
  71.